﻿using MediatR;
using System.Security.Cryptography;
using System.Xml.Linq;
using Traceability.App.Api.Read.Application.Command.Trace;
using Traceability.App.Api.Read.DTO;
using Traceability.App.Domain.新文件夹;
using Traceability.App.ErrorCode;
using Traceability.App.Intrastructure.Interface;

namespace Traceability.App.Api.Read.Application.Handler.Trace
{
    public class SuYuanDetailHandler : IRequestHandler<SuYuanDetailCommand, ApiResult<SuYuanDetailDTO>>
    {
        private readonly IBaseRepository<TraceabilityModel> traceRepo;
        private readonly IBaseRepository<ShopModel> shopRepo;

        public SuYuanDetailHandler(IBaseRepository<TraceabilityModel> traceRepo, IBaseRepository<ShopModel> shopRepo)
        {
            this.traceRepo = traceRepo;
            this.shopRepo = shopRepo;
        }
        public Task<ApiResult<SuYuanDetailDTO>> Handle(SuYuanDetailCommand request, CancellationToken cancellationToken)
        {
            var res = new ApiResult<SuYuanDetailDTO>();

            var l = from a in traceRepo.GetAll()
                    join b in shopRepo.GetAll()
                    on a.Sid equals b.Id
                    where a.IsDel == false
                    select new SuYuanDetailDTO
                    {

                        Sid = a.Sid,
                        Sname = b.Sname,
                        ProduceBatch = a.ProduceBatch,
                        Shelflife = a.Shelflife,
                        ProduceNumber = a.ProduceNumber,
                        Manufacturers = a.Manufacturers,
                        DegreeinFormation = a.DegreeinFormation,
                        Img = a.Img,

                    };
            var trac = l.FirstOrDefault(x => x.ProduceBatch == request.ProduceBatch);
            res.Message = "详情显示成功";
            res.Code = ApiEnum.Ok;
            res.Data = trac;
            return Task.FromResult(res);
        }
    }
}
